package com.atguigu.gmall.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;

import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;


/**
 * 跨域请求的配置
 */
@Configuration
public class CorsConfig {


    @Bean
    public CorsWebFilter corsWebFilter() {
        //初始化CORS配置对象
        CorsConfiguration config = new CorsConfiguration();
        //允许的域，不要写*，否则cookie无法使用 //如果页面启动时localhost，就不能配置127.0.0.1
        config.addAllowedOrigin("http://localhost:1000");
        //允许的头信息
        config.addAllowedHeader("*");
        //允许的请求方式
        config.addAllowedMethod("*");
        //是否允许携带Cookie信息
        config.setAllowCredentials(true);

        //添加映射路径，我们拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**",config);

        return new CorsWebFilter(source);
    }
}
